package cn.cscb.mms.server.controller;

import cn.cscb.mms.common.result.Result;
import cn.cscb.mms.pojo.entity.Customer;
import cn.cscb.mms.server.service.CustomerService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * 测试用控制器，无需扩展
 *
 * @author Gao Yipeng
 */
@Slf4j
@RestController("customerController")
@RequestMapping("/customer")
public class CustomerController {

    @Resource(name = "customerService")
    private CustomerService customerService;

    /**
     * 测试@Select注解方式查询
     *
     * @param id 客户信息逻辑主键
     * @return 客户信息
     */
    @GetMapping("/cust1")
    public Result<Customer> getByIdTest01(@RequestParam("id") Integer id) {
        return Result.success(customerService.getCustomerByIdTest01(id));
    }

    /**
     * 测试xml方式查询
     *
     * @param id 客户信息逻辑主键
     * @return 客户信息
     */
    @GetMapping("/cust2")
    public Result<Customer> getByIdTest02(@RequestParam("id") Integer id) {
        return Result.success(customerService.getCustomerByIdTest02(id));
    }

    /**
     * 测试分页查询
     *
     * @param page 当前页数
     * @param row  每页行数
     * @return 分页查询结果
     */
    @GetMapping("/custs")
    public Result<IPage<Customer>> getCustomerPages(@RequestParam("page") Integer page,
                                                    @RequestParam("row") Integer row) {
        return Result.success(customerService.getCustomerPages(page, row));
    }

    /**
     * 测试接口调用异常
     *
     * @return 客户信息（无意义）
     */
    @GetMapping("/error")
    public Result<Customer> testError() {
        return Result.success(customerService.testEncounterException());
    }
}
